package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Tjyydhx;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.domain.res.TjyydhxRes;
import com.siwei.apply.domain.vo.TjyydhxUpdateVo;
import com.siwei.apply.domain.vo.TjyydhxVo;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.TjyydhxMapper;
import com.siwei.apply.mapper.WorkflowMapper;
import com.siwei.apply.service.TjyydhxService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import static com.siwei.apply.common.Common.UserId;

/**
 * 规划条件与用地红线出具 服务实现类
 */
@Service
public class TjyydhxImpl implements TjyydhxService {

    @Autowired
    private TjyydhxMapper tjyydhxMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return tjyydhxMapper.isExit(projectId);
    }

    @Override
    public String add(TjyydhxVo tjyydhxVo) {
        Tjyydhx tjyydhx = new Tjyydhx();
        BeanUtils.copyProperties(tjyydhxVo, tjyydhx);
        tjyydhx.generateId();
        tjyydhx.setCreatorId(UserId);
        tjyydhxMapper.add(tjyydhx);

        // 添加项目流程
        Integer projectType = projectMapper.getProjectTypeById(tjyydhx.getProjectId());
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_tjyydhx");
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(tjyydhx.getId());
        projectWorkflow.setParentId("");
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(tjyydhx.getProjectId());
        projectWorkflow.setNodeId(tjyydhx.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getName());
        projectWorkflowMapper.add(projectWorkflow);
        return tjyydhx.getId();
    }

    @Override
    public TjyydhxRes get(String projectId) {
        Tjyydhx tjyydhx = tjyydhxMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        TjyydhxRes tjyydhxRes = new TjyydhxRes();
        if(tjyydhx != null){
            BeanUtils.copyProperties(tjyydhx, tjyydhxRes);
        }
        tjyydhxRes.setProjectId(projectId);
        tjyydhxRes.setProjectName(project.getName());
        tjyydhxRes.setProjectCode(project.getCode());
        return tjyydhxRes;
    }

    @Override
    public TjyydhxRes getById(String id) {
        Tjyydhx record = tjyydhxMapper.getById(id);
        if (record == null) {
            return null;
        }
        Project project = projectMapper.get(record.getProjectId());
        TjyydhxRes res = new TjyydhxRes();
        BeanUtils.copyProperties(record, res);
        res.setProjectId(record.getProjectId());
        res.setProjectName(project.getName());
        res.setProjectCode(project.getCode());
        return res;
    }

    @Override
    public void update(TjyydhxUpdateVo tjyydhxUpdateVo) {
        tjyydhxMapper.update(tjyydhxUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        tjyydhxMapper.updateHasOnchain(id, hasOnchain);
    }
}
